#include <stdio.h>
#include "functions.h"
#include "gui.h"
#include "mtcore.h"

int main(int argc, char **argv) {

	unsigned long int m, n;
	int w_to_file = 0;

	desc();
	states_desc();
	FILE *out = NULL;

	if(check_args(argc, argv) == 0) {
		out = stdout;
		fprintf(stdout, "\n\nPodaj m: \n");
		scanf("%d", &m);
		fprintf(stdout, "\n\nPodaj n: \n");
		scanf("%d", &n);
	} else if(check_args(argc, argv) == 1){
		w_to_file = 1;
		out = fopen(argv[4], "w+");
		m = atoi(argv[1]);
		n = atoi(argv[2]);
	} else
		proper_using();

	int tape_lgt = m + n + 2;
	int* tape = (int*) create_tape(m, n);
	if(tape == NULL)
		fprintf(stderr, "Nie udalo sie zaalokowac pamieci dla podanych liczb..\n");

	fprintf(stdout, "\n\n\nMaszyna wykonuje prace..\n");
	fprintf(out, "Tasma wejsciowa\n");
	fprint_tape(tape, tape_lgt, out);
	fprintf(out, "\n\nOpis chwilowy\n");
	ch_fprint_tape(tape, tape_lgt, out);
	
	if(w_to_file) {
		fprintf(stdout, "\n\nTasma wejsciowa oraz opis chwilowy dzialania MT zapisany zostal do pliku: %s\n", argv[4]);
		fclose(out);
	}


	fprintf(stdout,"\n\nWynikiem zadanych obliczen jest %d - %d = %d\n", m, n, count(tape, tape_lgt));
	
	return 0;
}
